package city.spring.modules.medicalstation.drug.service.impl;

import city.spring.modules.medicalstation.drug.entity.DrugEntity;
import city.spring.modules.medicalstation.drug.repository.DrugRepository;
import city.spring.modules.medicalstation.drug.service.DrugService;
import city.spring.modules.medicalstation.drug.vo.DrugVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：医护站药品信息
 *
 * @author 谢金婷
 * @date 2020-08-14 11:07:57
 */
@CacheConfig(cacheNames = {DrugServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class DrugServiceImpl extends ServiceImpl<DrugRepository, DrugEntity> implements DrugService {
    private final DrugRepository drugRepository;

    public DrugServiceImpl(DrugRepository drugRepository) {
        this.drugRepository = drugRepository;
    }

    @Override
    public void saveDrug(DrugEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateDrug(DrugEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteDrug(Integer primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteDrug(List<Integer> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<DrugVO> listPage(Page<? extends DrugEntity> page, Wrapper<? extends DrugEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }
}